package com.example.service;

import com.example.entity.Admin;
import com.example.entity.Dormitory;
import com.example.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;

    public boolean existsInDb(Admin admin){
        List<Admin> adminList = adminMapper.findByid(admin.getAdmin_num());
        return !adminList.isEmpty();
    }

    public int save(Admin admin){
        if(existsInDb(admin) ) {
            return adminMapper.update(admin);
        }else {
            return adminMapper.insert(admin);
        }
    }
    public boolean existsInDb1(Admin admin){
        List<Admin> adminList = adminMapper.findByid(admin.getAdmin_num());
        return !adminList.isEmpty();
    }

    public int save1(Admin admin){
        if(existsInDb(admin) ) {
            return adminMapper.update(admin);
        }else {
            return adminMapper.insert1(admin);
        }
    }

    public Map<String, Object> saveAdmin(Admin admin) {
        Map<String, Object> result = new HashMap<>();

        Integer saveResult = save1(admin);

        if (saveResult != null) {
            result.put("code", 200);
            result.put("message", "保存成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "保存失败");
            result.put("data", null);
        }

        return result;
    }

    public Map<String, Object> deleteAdmin(Integer admin_num) {
        Map<String, Object> result = new HashMap<>();

        Integer deleteResult = adminMapper.deleteBy(admin_num);

        if (deleteResult != null) {
            result.put("code", 200);
            result.put("message", "删除成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "删除失败");
            result.put("data", null);
        }

        return result;
    }

}
